package com.parse;

import java.util.HashMap;
import java.util.Map;

public class SymbolTable {
    private Map<String, Symbol> symbols;
    private SymbolTable parent;

    public SymbolTable() {
        symbols = new HashMap<>();
        parent = null;
    }

    public SymbolTable(SymbolTable parent) {
        symbols = new HashMap<>();
        this.parent = parent;
    }

    public void define(String name, Object value, int level) {
        Symbol symbol = new Symbol(name, value, level);
        symbols.put(name, symbol);
    }

    public Symbol resolve(String name) {
        Symbol symbol = symbols.get(name);
        if (symbol != null) {
            return symbol;
        } else if (parent != null) {
            return parent.resolve(name);
        } else {
            return null;
        }
    }

    public static class Symbol {
        private String name;
        private Object value;
        private int level;

        public Symbol(String name, Object value, int level) {
            this.name = name;
            this.value = value;
            this.level = level;
        }

        public String getName() {
            return name;
        }

        public Object getValue() {
            return value;
        }

        public void setValue(Object value) {
            this.value = value;
        }

        public int getLevel() {
            return level;
        }

        public void setLevel(int level) {
            this.level = level;
        }
    }
}
